package com.mx.project.services.impl;

import com.alibaba.fastjson.JSON;
import com.mx.project.dao.ProductMapper;
import com.mx.project.pojo.Product;
import com.mx.project.services.ProductService;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

@Log4j2
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<Product> getProductByCid(Integer cid,Integer pageNum) {
        List<Product> list = productMapper.selectByCid(cid);
        return list;
    }

    @Override
    public boolean importProductToESDoc(List<Product> list) {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10m");
        for (int i = 0; i < list.size(); i++) {
            bulkRequest.add(new IndexRequest("mx_product")
                    .source(JSON.toJSONString(list.get(i)), XContentType.JSON)
            );
        }
        BulkResponse bulk = null;
        try {
            bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error(e.getCause());
        }
        return !bulk.hasFailures();
    }

    @Cacheable(cacheNames = "products")
    @Override
    public List<Product> getAllProduct() {
        return productMapper.selectAll();
    }

    @Override
    public List<Map<String, Object>> SearchProduct(Integer pageNo, Integer count, String keyword) {
        SearchRequest searchRequest1 = new SearchRequest("mx_product");
        searchRequest1.source(new SearchSourceBuilder().query(matchPhraseQuery("name", keyword)));
        SearchRequest searchRequest2 = new SearchRequest("mx_product");
        searchRequest2.source(new SearchSourceBuilder().query(matchPhraseQuery("detail", keyword)));
        SearchResponse searchResponse1 = null;
        SearchResponse searchResponse2 = null;
        try {
            searchResponse1 = client.search(searchRequest1, RequestOptions.DEFAULT);
            searchResponse2 = client.search(searchRequest2, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error(e.getCause());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : searchResponse1.getHits().getHits()) {
            list.add(hit.getSourceAsMap());
        }
        for (SearchHit hit : searchResponse2.getHits().getHits()) {
            list.add(hit.getSourceAsMap());
        }
        return list;
    }

//    @Override
//    public int updateMark(Integer pid,String mark) {
//        return productMapper.updateMarkByPid(pid,mark);
//    }

    @Override
    public Product getProductByPid(Integer pid) {
        return productMapper.selectByPrimaryKey(pid);
    }

}
